﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Sagacity.NC.Business.SysMange;

namespace Sagacity.NC.Web
{
    public class PageBase:System.Web.UI.Page
    {
        /// <summary>
        /// 是否验证，是 或 否
        /// </summary>
        public bool IsAuthenticated
        {
            get
            {
                return FormUser.IsAuthenticated;
            }
        }

        /// <summary>
        /// 取得当前的使用者
        /// </summary>
        public FormUser CurrentUser
        {
            get
            {
                return FormUser.CurrentUser;
            }
        }

        /// <summary>
        /// 清除session对象
        /// </summary>
        [System.Web.Services.WebMethod()]
        public static void ClearGlobalSession()
        {
            HttpContext.Current.Session[GlobalSessionKey] = null;
        }

        public const string GlobalSessionKey = "SagacitySession";

        /// <summary>
        /// 写入Cookie对象
        /// </summary>
        /// <param name="uInfo">用户对象</param>
        /// <returns></returns>
        public void WriteCookie(Sagacity.Entity.Build.UserInfo uInfo)
        {
            string objString = uInfo.WriteObject();
            System.Web.HttpCookie cookieObj = null;
            HttpContext.Current.Response.Cookies.Remove("Sagacity.NC_User");
            if (cookieObj == null)
            {
                cookieObj = new HttpCookie("Sagacity.NC_User");
                cookieObj.HttpOnly = false;
                cookieObj.Expires = new DateTime(2019, 1, 1);
                HttpContext.Current.Response.Cookies.Add(cookieObj);
            }

            cookieObj.Value = HttpUtility.UrlEncode(objString);
        }

        /// <summary>
        /// 读取Cookie对象
        /// </summary>
        /// <returns>返回User对象</returns>
        public Sagacity.Entity.Build.UserInfo ReadCookie()
        {
            System.Web.HttpCookie cookie = HttpContext.Current.Request.Cookies["Sagacity.NC_User"];
            string objString = HttpUtility.UrlDecode(cookie.Value);
            return (new Sagacity.Entity.Build.UserInfo()).ReadObject(objString);
        }

        /// <summary>
        /// 初始化方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                string httpPath = Sagacity.Config.AppSetting.CurrentApp.HttpPath;
                Sagacity.Build.Sys.SysParameters.SetMainParameterValues(CurrentUser.BizExcuteDate, CurrentUser.LoginRole.Id,CurrentUser.Id);
                 //refresh onlines
                if (CurrentUser == null)
                    HttpContext.Current.Response.Redirect("~/login.aspx");
                CurrentUser.Refresh();
                base.OnLoad(e);
            }
            catch(Exception ce) { }
        }
    }
}